﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using Xpom_Xpum__Integrated_Development_Environment.Data.Configuration;
using Xpom_Xpum__Integrated_Development_Environment.Model;
using Xpom_Xpum__Integrated_Development_Environment.View;
using Xpom_Xpum__Integrated_Development_Environment.View.Controls;
using Xpom_Xpum__Integrated_Development_Environment.ViewModel;
using Xpom_Xpum__Integrated_Development_Environment.ViewModel.Controls;

namespace Xpom_Xpum__Integrated_Development_Environment
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        #region Constructor

        public App()
        {
            sw = new SplashWindowView
            {
            };

            sw.Show();

            var t = new Thread(new ThreadStart(Init));
            t.Start();

            this.Exit += new ExitEventHandler(App_Exit);
        }
        
        #endregion

        #region Commands

        #endregion

        #region Fields

        public ProjectExplorerViewModel pevm;

        public ChooseTemplateWindowViewModel ctwvm;
        public ChooseTemplateWindowView ctw;

        public NewItemWindowViewModel niwvm;
        public NewItemWindowView niwv;

        private SplashWindowView sw;

        public Process XCGProcess;

        public event DataReceivedEventHandler XCGOutputDataReceived;

        public ProjectExplorerView pev;

        public TabControlViewModel tcvwm;

        public IntegratorSettingsWindowViewModel iswvm;

        public MenuViewModel mvm;

        #endregion

        #region Properties

        #endregion

        #region Methods

        public void Init()
        {

            // Loading Global Settings...
            GlobalSettings.SDKPath = System.AppDomain.CurrentDomain.BaseDirectory;
            GlobalSettings.XCGPath = GlobalSettings.SDKPath +
                @"Environment\Xpom-Xpum! Code Generator\xcg.exe";

            // Loading Templates
            ctwvm = new ChooseTemplateWindowViewModel();
            Application.Current.Dispatcher.Invoke(DispatcherPriority.ApplicationIdle, new Action(() =>
            {
                ctw = new ChooseTemplateWindowView();

                var tmp = ctw.WindowStyle;
                var tmp2 = ctw.Width;
                var tmp3 = ctw.Height;

                ctw.WindowStyle = WindowStyle.None;
                ctw.Left = -100;
                ctw.Width = 0;
                ctw.Height = 0;

                ctw.ShowActivated = false;
                ctw.Show();
                ctw.Hide();
                ctw.ShowActivated = true;

                ctw.WindowStyle = tmp;
                ctw.Width = tmp2;
                ctw.Height = tmp3;
            }));

            // Loading Item Template
            niwvm = new NewItemWindowViewModel();
            Application.Current.Dispatcher.Invoke(DispatcherPriority.ApplicationIdle, new Action(() =>
            {
                niwv = new NewItemWindowView();

                var tmp = niwv.WindowStyle;
                var tmp2 = niwv.Width;
                var tmp3 = niwv.Height;

                niwv.WindowStyle = WindowStyle.None;
                niwv.Left = -100;
                niwv.Width = 0;
                niwv.Height = 0;

                niwv.ShowActivated = false;
                niwv.Show();
                niwv.Hide();
                niwv.ShowActivated = true;

                niwv.WindowStyle = tmp;
                niwv.Width = tmp2;
                niwv.Height = tmp3;
            }));

            // Loading Code Generator
            if (File.Exists(GlobalSettings.XCGPath))
            {
                XCGProcess = new Process();
                XCGProcess.StartInfo.Arguments = "-init \"" + GlobalSettings.SDKPath;
                XCGProcess.StartInfo.CreateNoWindow = true;
                XCGProcess.StartInfo.FileName = GlobalSettings.XCGPath;
                XCGProcess.StartInfo.RedirectStandardInput = true;
                XCGProcess.StartInfo.RedirectStandardOutput = true;
                XCGProcess.StartInfo.UseShellExecute = false;
                XCGProcess.OutputDataReceived += new DataReceivedEventHandler(XCGProcess_OutputDataReceived);
                XCGProcess.Start();
                ((App)Application.Current).XCGProcess.BeginOutputReadLine();
            }
            else
            {
                var contin = 
                    MessageBox.Show("Can't find Code Generator utility at path:\n" +
                                    GlobalSettings.XCGPath + "\n\n" +
                                    "Continue?",
                    "Error", MessageBoxButton.YesNo, MessageBoxImage.Error);
                if (contin == MessageBoxResult.No)
                {
                    Environment.Exit(1);
                    return;
                }
            }

            // Loading Tab Control View Model
            tcvwm = new TabControlViewModel();

            // Loading Menu
            mvm = new MenuViewModel();

            // Loading Browser Integrator Settings
            iswvm = new ViewModel.IntegratorSettingsWindowViewModel();

            // Show MainWindow
            Application.Current.Dispatcher.Invoke(DispatcherPriority.ApplicationIdle, new Action(() =>
            {
                var mw = new MainWindowView
                {
                };

                MainWindow = mw;

                mw.Show();

                sw.Hide();
            }));
        }

        private void XCGProcess_OutputDataReceived(object z, DataReceivedEventArgs e)
        {
            if ((XCGOutputDataReceived != null) && (e.Data != null))
                XCGOutputDataReceived(this, e);
        }

        private void App_Exit(object sender, ExitEventArgs e)
        {
            XCGProcess.Kill();
        }

        #endregion
    }
}
